﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using BestLifePOS_Core.DomainObjects;
using BestLifePOS_Core.Data.Core;
using BestLifePOS_Core.Exceptions.DataBase;
using System.Data;

namespace BestLifePOS_Core.Data.Commands
{
    class BestLifeSaveNewPODataAction : BestLifeDataAction<int>
    {

        private Purchase_Order GvPO;
        private string Gvxml;
        private List<string> GvList;

        public BestLifeSaveNewPODataAction(Purchase_Order PPO, string Pxml, List<string> PList)
        {

            GvPO = PPO;
            Gvxml = Pxml;
            GvList = PList;

        }

        public override int Body(SqlConnection PConn)
        {
            try
            {
                string LvS = string.Join(",", GvList);
                SqlCommand LvCommand = new SqlCommand();
                LvCommand.Connection = PConn;
                LvCommand.CommandType = CommandType.StoredProcedure;
                LvCommand.CommandText = "dbo.InsertPODetails";

                LvCommand.Parameters.Add(new SqlParameter("@PO_No", GvPO.Po_Id.ToString()));
                LvCommand.Parameters.Add(new SqlParameter("@Sup_ID", GvPO.Sup_Id.ToString()));
                LvCommand.Parameters.Add(new SqlParameter("@Branch_ID", GvPO.Branch_Id.ToString()));
                LvCommand.Parameters.Add(new SqlParameter("@Description", GvPO.Description.ToString()));
                LvCommand.Parameters.Add(new SqlParameter("@User_Create", GvPO.User_Id.ToString()));
                LvCommand.Parameters.Add(new SqlParameter("@Create_Date", DateTime.Parse(GvPO.Po_Date.ToString())));

                LvCommand.Parameters.Add(new SqlParameter("@podetails", Gvxml));
                LvCommand.Parameters.Add(new SqlParameter("@prlist", LvS));


                int LvCount = LvCommand.ExecuteNonQuery();
                return LvCount; 

            }
            catch (BestlifeDatabaseException e)
            {
                throw new BestlifeDatabaseException(e);
            }
        }
    }
}
